<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 订单管理
 * @author zxj
 *
 */
class MyOrder extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('franchisees_model');
    }


    function outputCustom(){
        $formData = gp();
        $sql = '
        select a.id,a.oid,a.order_num,a.product_brand,a.product_model,a.productSku,a.brandCustomerNum,a.status_id,a.source_id_order,
        b.source_oid,b.merchant_num,b.consumer_email,b.create_time,a.imei,a.add_time
        from mt_product as a
        left join mt_order as b on a.oid = b.id
        where b.merchant_num in(11,12) and b.storeOrShip=0 and b.create_time>=\'2016-07-01\' and b.create_time<=\'2016-12-31\'
        ';
        $dataList = $this->common_model->getQuery($sql,'result_array');
        foreach ($dataList as &$rs) {
            $statusOne = $this->common_model->selectFirst('mt_product_status',array('id'=>$rs['status_id']));
            $rs['statusName'] = $statusOne['name_ch'];
            if($rs['brandCustomerNum']){
                $brandCustom = $this->common_model->selectFirst('mt_customers',array('id'=>$rs['brandCustomerNum']));
                $rs['brandCustomName'] = $brandCustom['name'];
            }
            $merchantCustom = $this->common_model->selectFirst('mt_customers',array('id'=>$rs['merchant_num']));
            $rs['merchantName'] = $merchantCustom['name'];
            $rs['updateData'] = date('Y-m-d H:i:s',$rs['add_time']);
        }
        unset($rs);
        $fieldData = array(
            'id' =>'产品ID','order_num'=>'维修单号','merchantName'=>'电商客户','brandCustomName'=>'品牌客户',
            'source_oid'=>'来源单号','source_id_order'=>'购买单号','statusName'=>'状态','updateData'=>'更新日期',
            'product_brand'=>'产品品牌','product_model'=>'产品型号','imei'=>'IMEI',
            'consumer_email'=>'客户邮箱','create_time'=>'创建日期'
        );
        $title = count($dataList).'_'.date('YmdHis');
        excel_output($title,$fieldData,$dataList);


    }


    /**
     * 个人主页
     */
    function index()
    {
        $data = gp();
        $account = $data['account'] ? $data['account'] : $this->users['account'];
        $role = $data['role'] ? $data['role'] : $this->users['role'];
        $where = $whereIn = array();
        if ($role == 'service') {
            $whereIn = array('status_id' => array(1, 14, 17));
        } elseif ($role == 'repairer') {
            $where = array('technician' => $account);
            $whereIn = array('status_id' => array(2, 3, 6, 7, 9));
        } elseif ($role == 'qc') {
            $whereIn = array('status_id' => array(10, 11, 12, 15, 16, 14, 17));;
        }
        //分页
        $searchStr = 'orders/MyOrder/?';
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url($searchStr);
        $config['total_rows'] = $this->common_model->countRows('mt_product', $where, $whereIn);
        $config['per_page'] = 20;
        $page = max(1, intval($data['page']));
        $config['uri_segment'] = 5;
        $slimit = ($page - 1) * $config['per_page'];
        $this->pagination->initialize($config);
        $data['show_pages'] = $this->pagination->create_links();
        $data['total_rows'] = $config['total_rows'];
        //$total_page = ceil($config['total_rows'] / $config['per_page']);
        //$data['show_params'] = "totalRows:{$config['total_rows']} {$config['per_page']}   {$page}/{$total_page}";
        $dataList = $this->common_model->select('mt_product', $slimit, $config['per_page'], 'id desc',$where, $whereIn);
        foreach ($dataList as $key=>&$rs) {
            $orders = $this->common_model->selectFirst('mt_order', array('id' => $rs['oid']));
            $rs['source_oid'] = $orders['source_oid'];
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orders['service_type']));
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $rs['category_id']));
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $rs['status_id']));
            $rs['serviceName'] = $serviceType['name_' . $this->curLang];
            $rs['categoryName'] = $categoryFirst['name_' . $this->curLang];
            $rs['statusName'] = $statusFirst['name_' . $this->curLang];
            $rs['add_time'] = 'updateDate:' . date('Y-m-d H:i:s', $rs['add_time']);
            $rs['holdColorOrder'] = $rs['orderAvailable'] == 2?'#ff0909':'#0000ff';
            $rs['holdColorProduct'] = $rs['available'] == 2?'#ff0909':'#0000ff';
            $rs['nextAct'] = $this->common_model->flowAction($rs['id'], $rs['status_id']);
        }
        unset($rs);
        $data['dataList'] = $dataList;
        //视图以及样式
        $this->load->view('defaults/myorder_list', $data);
        $this->load->view('defaults/common/footer');

    }


    /**
     * 待维修
     */
    function stayRepair()
    {
        $data = gp();
        $data['post_type'] = 'stayRepair';
        $keyword = addslashes(trim($data['keyword']));
        //默认select设置
        $service_center = intval($data['service_center']);
        $optServiceCenter[$service_center] = 'selected';
        $data['optServiceCenter'] = $optServiceCenter;
        $status_id = intval($data['status_id']);
        $optStatus[$status_id] = 'selected';
        $data['optStatus'] = $optStatus;
        //查询条件
        $searchStr = 'orders/MyOrder/stayRepair/?';
        $where = ' where (b.status_id=6 or b.status_id=7) ';
        if($status_id){
            $where = " where b.status_id={$status_id} ";
            $searchStr .= '&status_id=' . $status_id;
        }
        if($service_center){
            $where .= " and c.service_center=$service_center ";
            $searchStr .= '&service_center=' . $service_center;
        }
        if($keyword){
            $where .= " and (c.order_num = '{$keyword}' or c.source_oid='{$keyword}' or b.imei='{$keyword}' or b.productSku='{$keyword}') ";
            $searchStr .= '&keyword=' . $keyword;
        }
        //分页
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url($searchStr);
        $sql = 'select b.* from mt_product as b left join mt_order as c on b.oid = c.id  '.$where;
        $config['total_rows'] = $this->common_model->getQuery($sql, 'num_rows');
        $config['per_page'] = $data['outputStr']?$config['total_rows']:20;
        $page = $data['outputStr']?1:max(1, intval($data['page']));
        $config['uri_segment'] = 5;
        $offset = ($page - 1) * $config['per_page'];
        $this->pagination->initialize($config);
        $data['show_pages'] = $this->pagination->create_links();
        $data['total_rows'] = $config['total_rows'];
        //$total_page = ceil($config['total_rows'] / $config['per_page']);
        //$data['show_params'] = "totalRows:{$config['total_rows']} {$config['per_page']}   {$page}/{$total_page}";
        $sql .= ' order by b.add_time desc limit ' . $offset . ',' . $config['per_page'] . ' ';
        $dataList = $this->common_model->getQuery($sql, 'result_array');
        $conditionArr = array(1=>'全新',2=>'九成新',4=>'五成新',6=>'严重损坏');
        foreach ($dataList as $key=>&$rs) {
            $orders = $this->common_model->selectFirst('mt_order', array('id' => $rs['oid']));
            $rs['source_oid'] = $orders['source_oid'];
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orders['service_type']));
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $rs['category_id']));
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $rs['status_id']));
            $rs['serviceName'] = $serviceType['name_' . $this->curLang];
            $rs['categoryName'] = $categoryFirst['name_' . $this->curLang];
            $rs['statusName'] = $statusFirst['name_' . $this->curLang];
            $rs['condition'] = $conditionArr[$rs['physicalAppearance']];
            $rs['create_time'] = date('Y-m-d H:i:s', $rs['add_time']);
            $rs['add_time'] = 'updateDate:' . date('Y-m-d H:i:s', $rs['add_time']);
            $rs['holdColorOrder'] = $rs['orderAvailable'] == 2?'#ff0909':'#0a8e61';
            $rs['holdColorProduct'] = $rs['available'] == 2?'#ff0909':'#0a8e61';
            $rs['nextAct'] = $this->common_model->flowAction($rs['id'], $rs['status_id']);
            //导出数据
            if($data['outputStr']){
                $rs['laborAmount'] = $rs['laborAmountBrand'] + $rs['laborAmountIndividualUser'] + $rs['laborAmountMerchant'];
                $rs['partsAmount'] = $rs['partsAmountMerchant'] + $rs['partsAmountIndividualUser'] + $rs['partsAmountBrand'];
                $rs['shippingAmount'] = $rs['shippingFeeAmountBrand'] + $rs['shippingFeeAmountMerchant'] + $rs['shippingFeeAmountIndividualUser'];
                $rs['shippingAmount'] = $rs['shippingFeeAmountBrand'] + $rs['shippingFeeAmountMerchant'] + $rs['shippingFeeAmountIndividualUser'];
                $rs['storeOrShipName'] = $rs['storeOrShip']?'发货':'入库';
                if($orders['merchant_num']){
                    $customers = $this->common_model->selectFirst('mt_customers', array('id' => $orders['merchant_num']));
                }
                $rs['merchantName'] = $customers?$customers['name']:'';
                if($rs['brandCustomerNum']){
                    $brandCustomers = $this->common_model->selectFirst('mt_customers', array('id' => $orders['brandCustomerNum']));
                }
                $rs['brandName'] = $brandCustomers?$brandCustomers['name']:'';
                $rs['seller_warranty'] = $rs['seller_warranty']?'Yes':'No';
                $rs['inWarranty_qc'] = $rs['inWarranty_qc']?'Yes':'No';
                $rs['inWarranty'] = $rs['inWarranty']?'Yes':'No';
                $rs['consumer_email'] = $orders['consumer_email'];
                $rs['currency_code'] = $orders['currency_code'];
                //#配件
                $parts = $this->common_model->selectCustom('mt_product_parts', array('pid' => $rs['id']));
                $rs['partsCount'] = count($parts);
                foreach ($parts as $pj) {
                    $rs['partSku'] .= $pj['sku'].'        |';
                    $rs['partName'] .= $pj['name'].'      |';
                }
            }
        }
        unset($rs);
        $data['dataList'] = $dataList;
        //查询条件
        $serviceCenterList = $this->common_model->selectall('mt_service_center');
        $data['serviceCenterList'] = $serviceCenterList;
        //查询所有状态
        $statusList = $this->common_model->selectCustom('mt_product_status','','',array('id'=>array(6,7)));
        $data['statusList'] = $statusList;
        //视图以及样式
        $data['curLang'] = $this->curLang;
        //导出数据
        $data['outputUrl'] = blang_url($searchStr.'&outputStr=true');
        if ($data['outputStr']) {
            $fieldData = array(
                'create_time' =>'日期','order_num'=>'维修单号','merchantName'=>'电商客户','brandName'=>'品牌客户',
                'id'=>'产品ID','product_model'=>'产品型号','imei'=>'IMEI','storeOrShipName'=>'处理方式','condition'=>'成色',
                'seller_warranty'=>'平台是否质保','inWarranty_qc'=>'拆包质检是否质保','inWarranty'=>'维修师是否质保',
                'source_oid'=>'来源单号','serviceName'=>'维修类型','statusName'=>'状态','consumer_email'=>'客户邮箱',
                'laborAmount'=>'手工费','partsAmount'=>'配件费','shippingAmount'=>'运费','currency_code'=>'货币',
                'totalAmount'=>'总额','discountSubAmount'=>'优惠金额','partsCount'=>'配件数量','partSku'=>'配件SKU','partName'=>'配件名称'
            );
            excel_output($data['post_type'], $fieldData, $dataList);
        }else{
            $this->load->view('defaults/myorder_list', $data);
            $this->load->view('defaults/common/footer');
        }

    }


    /**
     * 配件已到货
     */
    function stayArrival()
    {
        $data = gp();
        $data['post_type'] = 'stayArrival';
        $keyword = addslashes(trim($data['keyword']));
        //默认select设置
        $service_center = intval($data['service_center']);
        $optServiceCenter[$service_center] = 'selected';
        $data['optServiceCenter'] = $optServiceCenter;
        //查询条件
        $searchStr = 'orders/MyOrder/stayArrival/?';
        $where = ' where b.status_id=9 and a.end_sid=8 ';
        if($service_center){
            $where .= " and c.service_center=$service_center ";
            $searchStr .= '&service_center=' . $service_center;
        }
        if($keyword){
            $where .= " and (c.order_num = '{$keyword}' or c.source_oid='{$keyword}' or b.imei='{$keyword}' or b.productSku='{$keyword}') ";
            $searchStr .= '&keyword=' . $keyword;
        }
        //分页
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url($searchStr);
        $sql = 'select b.* from mt_product_track as a left join mt_product as b on a.pid=b.id left join mt_order as c on a.oid = c.id '.$where;
        $config['total_rows'] = $this->common_model->getQuery($sql, 'num_rows');
        $config['per_page'] = $data['outputStr']?$config['total_rows']:20;
        $page = $data['outputStr']?1:max(1, intval($data['page']));
        $config['uri_segment'] = 5;
        $offset = ($page - 1) * $config['per_page'];
        $this->pagination->initialize($config);
        $data['show_pages'] = $this->pagination->create_links();
        $data['total_rows'] = $config['total_rows'];
        //$total_page = ceil($config['total_rows'] / $config['per_page']);
        //$data['show_params'] = "totalRows:{$config['total_rows']} {$config['per_page']}   {$page}/{$total_page}";
        $sql .= ' order by b.add_time desc limit ' . $offset . ',' . $config['per_page'] . ' ';
        $dataList = $this->common_model->getQuery($sql, 'result_array');
        $conditionArr = array(1=>'全新',2=>'九成新',4=>'五成新',6=>'严重损坏');
        foreach ($dataList as $key=>&$rs) {
            $orders = $this->common_model->selectFirst('mt_order', array('id' => $rs['oid']));
            $rs['source_oid'] = $orders['source_oid'];
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orders['service_type']));
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $rs['category_id']));
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $rs['status_id']));
            $rs['serviceName'] = $serviceType['name_' . $this->curLang];
            $rs['categoryName'] = $categoryFirst['name_' . $this->curLang];
            $rs['statusName'] = $statusFirst['name_' . $this->curLang];
            $rs['condition'] = $conditionArr[$rs['physicalAppearance']];
            $rs['create_time'] = date('Y-m-d H:i:s', $rs['add_time']);
            $rs['add_time'] = 'updateDate:' . date('Y-m-d H:i:s', $rs['add_time']);
            $rs['holdColorOrder'] = $rs['orderAvailable'] == 2?'#ff0909':'#0a8e61';
            $rs['holdColorProduct'] = $rs['available'] == 2?'#ff0909':'#0a8e61';
            $rs['nextAct'] = $this->common_model->flowAction($rs['id'], $rs['status_id']);
            //导出数据
            if($data['outputStr']){
                $rs['laborAmount'] = $rs['laborAmountBrand'] + $rs['laborAmountIndividualUser'] + $rs['laborAmountMerchant'];
                $rs['partsAmount'] = $rs['partsAmountMerchant'] + $rs['partsAmountIndividualUser'] + $rs['partsAmountBrand'];
                $rs['shippingAmount'] = $rs['shippingFeeAmountBrand'] + $rs['shippingFeeAmountMerchant'] + $rs['shippingFeeAmountIndividualUser'];
                $rs['shippingAmount'] = $rs['shippingFeeAmountBrand'] + $rs['shippingFeeAmountMerchant'] + $rs['shippingFeeAmountIndividualUser'];
                $rs['storeOrShipName'] = $rs['storeOrShip']?'发货':'入库';
                if($orders['merchant_num']){
                    $customers = $this->common_model->selectFirst('mt_customers', array('id' => $orders['merchant_num']));
                }
                $rs['merchantName'] = $customers?$customers['name']:'';
                if($rs['brandCustomerNum']){
                    $brandCustomers = $this->common_model->selectFirst('mt_customers', array('id' => $orders['brandCustomerNum']));
                }
                $rs['brandName'] = $brandCustomers?$brandCustomers['name']:'';
                $rs['seller_warranty'] = $rs['seller_warranty']?'Yes':'No';
                $rs['inWarranty_qc'] = $rs['inWarranty_qc']?'Yes':'No';
                $rs['inWarranty'] = $rs['inWarranty']?'Yes':'No';
                $rs['consumer_email'] = $orders['consumer_email'];
                $rs['currency_code'] = $orders['currency_code'];
                //#配件
                $parts = $this->common_model->selectCustom('mt_product_parts', array('pid' => $rs['id']));
                $rs['partsCount'] = count($parts);
                foreach ($parts as $pj) {
                    $rs['partSku'] .= $pj['sku'].'       |';
                    $rs['partName'] .= $pj['name'].'     |';
                }
            }
        }
        unset($rs);
        $data['dataList'] = $dataList;
        //查询条件
        $serviceCenterList = $this->common_model->selectall('mt_service_center');
        $data['serviceCenterList'] = $serviceCenterList;
        //视图以及样式
        $data['curLang'] = $this->curLang;
        //导出数据
        $data['outputUrl'] = blang_url($searchStr.'&outputStr=true');
        if ($data['outputStr']) {
            $fieldData = array(
                'create_time' =>'日期','order_num'=>'维修单号','merchantName'=>'电商客户','brandName'=>'品牌客户',
                'id'=>'产品ID','product_model'=>'产品型号','imei'=>'IMEI','storeOrShipName'=>'处理方式','condition'=>'成色',
                'seller_warranty'=>'平台是否质保','inWarranty_qc'=>'拆包质检是否质保','inWarranty'=>'维修师是否质保',
                'source_oid'=>'来源单号','serviceName'=>'维修类型','statusName'=>'状态','consumer_email'=>'客户邮箱',
                'laborAmount'=>'手工费','partsAmount'=>'配件费','shippingAmount'=>'运费','currency_code'=>'货币',
                'totalAmount'=>'总额','discountSubAmount'=>'优惠金额','partsCount'=>'配件数量','partSku'=>'配件SKU','partName'=>'配件名称'
            );
            excel_output($data['post_type'], $fieldData, $dataList);
        }else{
            $this->load->view('defaults/myorder_list', $data);
            $this->load->view('defaults/common/footer');
        }

    }





    /**
     *
     * 超时订单
     */
    public function timeoutOrder(){
        $gp = gp();
        $gp['page'] = $gp['page'] ? $gp['page'] : 1;
        $statusId=explode('or',$gp['status_id']);
        $statusIdArr=implode(',',$statusId);
        $gp['manager_id']?:$gp['manager_id']=$this->users['id'];
        $role_id=$this->common_model->selectFirst('user',array('id'=>$gp['manager_id']));
        $role_array=explode(',',$role_id['role_id']);
        if($gp['manager_id']) {
            if($gp['manager_id']==38){
                $cusStr='';
            }else{
                //查询当前客户经理下所属客户
                $cusData = $this->common_model->selectCustom('mt_customers', array('manager_id' => $gp['manager_id'], 'type' => 1));
                $cusArray = array();
                if($cusData) {
                    foreach ($cusData as $cs) {
                        $cusArray[] = $cs['id'];
                    }
                    $cusStr = implode(',', $cusArray);
                }else{
                    $cusStr = 11111111111111111;
                    if(!in_array(12,$role_array)){
                        $cusStr='';
                    }
                }
            }
        }
        $str = "?keyword=" . $gp['keyword'] . "&status_id=" . $statusIdArr. "&point=" . $gp['point'].'&client_id='.$gp['client_id'].'&manager_id='.$gp['manager_id'];
        $orderList = $this->franchisees_model->timeoutOrder(trim($gp['keyword']),$statusIdArr,$gp['point'],$gp['client_id'],$cusStr);
        $storeOrShip=array('入库','发货');
        $warranty=array('保外','保内');
        $damage=array(1=>'全新',2=>'九成新',4=>'五成新',6=>'严重损坏');
        //重构数据
        foreach ($orderList as $key => &$rs) {
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $rs['service_type']));
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $rs['category_id']));
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $rs['status_id']));
            if($gp['export']){
                $merchant_num=$this->common_model->selectFirst('mt_customers', array('id' => $rs['merchant_num']));
                $brandCustomerNum=$this->common_model->selectFirst('mt_customers', array('id' => $rs['brandCustomerNum']));
                $parts=$this->common_model->selectFirst('mt_product_parts', array('pid' => $rs['id']));
                $rs['merchant_num']=$merchant_num['name'];
                $rs['brandCustomerNum']=$brandCustomerNum['name'];
                $rs['quantity']=$parts['quantity'];
                $rs['sku']=$parts['sku'];
                $rs['name']=$parts['name'];
                $rs['laborAmount']=$rs['laborAmountMerchant']+$rs['laborAmountIndividualUser']+$rs['laborAmountBrand'];
                $rs['shippingFeeAmount']=$rs['shippingFeeAmountBrand']+$rs['shippingFeeAmountMerchant']+$rs['shippingFeeAmountIndividualUser'];
                $rs['partsAmount']=$rs['partsAmountMerchant']+$rs['partsAmountBrand']+$rs['partsAmountIndividualUser'];
                $rs['seller_warranty']=$warranty[$rs['seller_warranty']];
                $rs['inWarranty_qc']=$warranty[$rs['inWarranty_qc']];
                $rs['inWarranty']=$warranty[$rs['inWarranty']];
                $rs['physicalAppearance']=$damage[$rs['physicalAppearance']];
            }
            $rs['serviceName'] = $serviceType['name_' . $this->curLang];
            $rs['categoryName'] = $categoryFirst['name_' . $this->curLang];
            $rs['statusName'] = $statusFirst['name_' . $this->curLang];
            $rs['add_time'] = $rs['add_time']?date('Y-m-d H:i:s', $rs['add_time']):$rs['create_time'];
            $rs['holdColorOrder'] = $rs['orderAvailable'] == 2 ? '#ff0909' : '#0a8e61';
            $rs['holdColorProduct'] = $rs['available'] == 2 ? '#ff0909' : '#0a8e61';
            $rs['storeOrShip'] = $storeOrShip[$rs['storeOrShip']];
            //追踪号规则
            $rs['isInTrackNum'] = true;
            if ($rs['status_id'] == 14 && $rs['shipoutTrack']) {
                $rs['isInTrackNum'] = false;
            }
            $timeout=$this->timeout($rs['status_id'],$rs['add_time']);
            if(!$timeout){
                unset($orderList[$key]);
            }
        }
        unset($rs);
        if($gp['export']){
            $this->exportData($orderList);
            exit;
        }
        //分页查询
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url('orders/MyOrder/timeoutOrder' . $str);
        $config['uri_segment'] = config_item('enable_lang') ? 5 : 4;
        $config['per_page'] = 20;
        $config['total_rows'] = count($orderList);
        $this->pagination->initialize($config);
        $data['show_pages'] = $this->pagination->create_links();
        //分页结束

        $statusList = $this->common_model->selectCustom('mt_product_status');
        $data['statusList'] = $statusList;
        $data['pointList'] = $this->franchisees_model->GetPointList();
        $data['point']=$gp['point'];
        $data['statusId']=$gp['status_id'];
        $data['client_id']=$gp['client_id'];
        if($role_id['role_id']==12 || $gp['manager_id']==38){
            $data['manager_id']=$gp['manager_id'];
        }else{
            $data['manager_id']='';
        }
        $data['str']=$str;
        $data['customersList'] = $this->franchisees_model->getCustomersList(array(1));
        $orderListView=array_slice($orderList,($gp['page'] - 1) * $config['per_page'],20);
        $data['orderList'] = $orderListView;
        $data['curLang'] = $this->curLang;
        $data['show_params'] = $config['total_rows'];
        $data['salesList']=$this->common_model->selectCustom('user','','',array('role_id'=>12));
        $this->load->view('defaults/specialOrder/timeoutOrderList', $data);
        $this->load->view('defaults/common/footer');
    }

    /**
     * 超时检测
     * @param $status_id
     * @param $add_time
     * @return string
     */
    public function timeout($status_id,$add_time){
        $role_id=$this->users['role_id'];
        $str=0;
        $role_array=explode(',',$role_id);
        if(in_array(12,$role_array)){
            $role_id=12;
        }else{
            $role_id=2;
        }
        if(in_array($role_id,array(1,2,12))) {
            //超时数据
            $timeoutData[1] = array(1 => 7, 18 => 2,3=>2, 2 => 2,4 => 2,5 => 2, 6 => 2, 7 => 2, 8 => 10,9=>2, 10 => 2,11=>2, 12 => 2, 14=>2,15 => 2, 16 => 2);
            $timeoutData[2] = array(1 => 7, 18 => 2,3=>2, 2 => 2,4 => 2,5 => 2, 6 => 2, 7 => 2, 8 => 10,9=>2, 10 => 2, 12 => 2,11=>2, 14=>2,15 => 2, 16 => 2);
            $timeoutData[12] = array(1 => 10, 18 => 5,3=>5, 2 => 5, 4 => 5, 5 => 5, 6 => 5, 7 => 5, 8 => 10,9=>5, 10 => 5, 12 => 5,11=>5,14=>5, 15 => 5, 16 => 5);
            if(in_array($status_id,array_keys($timeoutData[$role_id]))) {
                $timeDiff = $this->getWorkday(date('Y-m-d H:i:s',time()),$add_time);
                $timeout = $timeoutData[$role_id][$status_id];
                if ($timeDiff > $timeout) {
                    $str = 1;
                }
            }
        }
        return $str;
    }

    /**
     * 获取双休日量
     * @param string $day_max
     * @param string $day_min
     * @return int
     */
    public function GetWeekendDay($day_max='',$day_min=''){
        $s=0;
        $day_max=explode(' ',$day_max);
        $day_max=strtotime($day_max[0]);
        $day_min=explode(' ',$day_min);
        $day_min=strtotime($day_min[0]);
        if($day_max && $day_min) {
            $day_time = 3600 * 24;
            $day_diff = ceil(($day_max - $day_min) / $day_time);
            for ($i = 0; $i <= $day_diff; $i++) {
                $day_w = date('w', $day_min + $i * $day_time);
                if ($day_w == 0 || $day_w == 6) {
                    $s++;
                }
            }
        }
        return $s;
    }

    /**
     * 获取工作日量
     * @param string $maxTime
     * @param string $minTime
     * @return float|int
     */
    public function getWorkday($maxTime='',$minTime=''){
        $day=0;
        if($maxTime!='' && $minTime!=''){
            $weekendDay=$this->GetWeekendDay($maxTime,$minTime);
            $day=round((strtotime($maxTime) - strtotime($minTime)) / (3600 * 24),1)-$weekendDay;
            if($day < 0){
                $day=0;
            }
        }
        return $day;
    }

    /**
     * 导出数据
     * @param $data
     */
    public function exportData($data){
        $fieldData=array(
            'add_time'=>'日期',
            'order_num'=>'维修单号',
            'merchant_num'=>'电商客户',
            'brandCustomerNum'=>'品牌客户',
            'id'=>'产品ID',
            'product_model'=>'产品型号',
            'imei'=>'IMEI',
            //'color'=>'颜色',
            'storeOrShip'=>'处理方式',
            'physicalAppearance'=>'成色',
            'seller_warranty'=>'商家质保',
            'inWarranty_qc'=>'拆包质保',
            'inWarranty'=>'维修师质保',
            'source_oid'=>'来源单号',
            'serviceName'=>'维修类型',
            'statusName'=>'状态',
            'consumer_email'=>'客户邮箱',
            'laborAmount'=>'手工费',
            'shippingFeeAmount'=>'运费',
            'partsAmount'=>'配件费',
            'currency_code'=>'货币种类',
            'grandTotal'=>'总额',
            'discountSubAmount'=>'优惠金额',
            'quantity'=>'配件数量',
            'sku'=>'配件SKU',
            'name'=>'配件名称'
        );
        excel_output('timeoutOrder',$fieldData,$data);
    }
}
